ostbuild: Create deploy-qemu subcommand
authorColin Walters <walters@verbum.org>
Thu, 3 May 2012 15:16:08 +0000 (11:16 -0400)
committerColin Walters <walters@verbum.org>
Fri, 4 May 2012 20:16:16 +0000 (16:16 -0400)
Makefile-ostbuild.am
gnomeos/gnomeos-qemu-create.sh [deleted file]
gnomeos/gnomeos-qemu-pull.sh [deleted file]
src/ostbuild/pyostbuild/builtin_deploy_qemu.py
src/ostbuild/pyostbuild/builtin_privhelper_deploy_qemu.py
src/ostbuild/pyostbuild/main.py
src/ostbuild/pyostbuild/privileged_subproc.py

index 0c87e65c66d6f730466cad1dd3e47e946c8a741c..a6e970626cda987cfe61d62d4776e06d94e3c3e9 100644 (file)
@@ -29,8 +29,10 @@ pyostbuild_PYTHON =                                  \
        src/ostbuild/pyostbuild/builtin_compose.py      \
        src/ostbuild/pyostbuild/builtin_chroot_compile_one.py   \
        src/ostbuild/pyostbuild/builtin_compile_one.py  \
+       src/ostbuild/pyostbuild/builtin_deploy_qemu.py  \
        src/ostbuild/pyostbuild/builtin_deploy_root.py  \
        src/ostbuild/pyostbuild/builtin_pull_components.py      \
+       src/ostbuild/pyostbuild/builtin_privhelper_deploy_qemu.py       \
        src/ostbuild/pyostbuild/builtin_git_mirror.py   \
        src/ostbuild/pyostbuild/builtin_prefix.py       \
        src/ostbuild/pyostbuild/builtin_resolve.py      \
diff --git a/gnomeos/gnomeos-qemu-create.sh b/gnomeos/gnomeos-qemu-create.sh
deleted file mode 100755 (executable)
index bc0e6a5..0000000
+++ /dev/null
@@ -1,74 +0,0 @@
-#!/bin/bash
-# -*- indent-tabs-mode: nil; -*-
-# Create ostree-qemu.img file in the current directory, suitable
-# for booting via qemu.
-#
-# Copyright (C) 2011,2012 Colin Walters <walters@verbum.org>
-#
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2 of the License, or (at your option) any later version.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the
-# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-# Boston, MA 02111-1307, USA.
-
-set -e
-set -x
-
-SRCDIR=`dirname $0`
-WORKDIR=`pwd`
-
-if test $(id -u) != 0; then
-    cat <<EOF
-This script should be run as root.
-EOF
-    exit 1
-fi
-
-usage () {
-    echo "$0"
-    exit 1
-}
-
-OBJ=ostree-qemu.img
-if ! test -f ${OBJ}; then
-    # Hardcoded 6 gigabyte filesystem size here; 6 gigabytes should be
-    # enough for everybody.
-    qemu-img create $OBJ 6G
-    mkfs.ext4 -q -F $OBJ
-fi
-
-mkdir -p fs
-umount fs || true
-sleep 1 # Avoid Linux kernel bug, pretty sure it's the new RCU pathname lookup
-mount -o loop ostree-qemu.img fs
-
-cd fs
-
-if ! test -d ./ostree/repo/objects; then
-    mkdir -p ./ostree
-    
-    $SRCDIR/gnomeos-setup.sh $(pwd)/ostree
-fi
-
-mkdir -p ./run ./home ./root ./sys
-mkdir -p ./tmp 
-chmod 01777 ./tmp
-
-mkdir -p $(pwd)/ostree/modules
-rsync -a -H -v --delete /ostree/modules/ ./ostree/modules/
-
-cd ..
-umount fs
-
-cat << EOF
-Next, run gnomeos-qemu-pull.sh to copy data.
-EOF
diff --git a/gnomeos/gnomeos-qemu-pull.sh b/gnomeos/gnomeos-qemu-pull.sh
deleted file mode 100755 (executable)
index 29c87aa..0000000
+++ /dev/null
@@ -1,70 +0,0 @@
-#!/bin/bash
-# -*- indent-tabs-mode: nil; -*-
-# Run built image in QEMU 
-#
-# Copyright (C) 2011,2012 Colin Walters <walters@verbum.org>
-#
-# This library is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2 of the License, or (at your option) any later version.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the
-# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-# Boston, MA 02111-1307, USA.
-
-set -e
-set -x
-
-SRCDIR=`dirname $0`
-WORKDIR=`pwd`
-
-if test $(id -u) != 0; then
-    cat <<EOF
-This script should be run as root.
-EOF
-    exit 1
-fi
-
-usage () {
-    cat <<EOF 
-usage: $0 SRC_REPO_PATH CURRENT_REF [REFS...]
-EOF
-    exit 1
-}
-
-SRC_REPO_PATH=$1
-test -n "$SRC_REPO_PATH" || usage
-shift
-
-CURRENT_REF=$1
-test -n "$CURRENT_REF" || usage
-shift
-
-if ! test -f ostree-qemu.img; then
-    cat <<EOF
-ostree-qemu.img not found; You must run gnomeos-qemu-create.sh first
-EOF
-fi
-
-mkdir -p fs
-umount fs || true
-sleep 1 # Avoid Linux kernel bug, pretty sure it's the new RCU pathname lookup
-mount -o loop ostree-qemu.img fs
-
-cd fs
-ostree --repo=./ostree/repo pull-local ${SRC_REPO_PATH} ${CURRENT_REF} "$@"
-
-cd ostree
-ostree --repo=./repo checkout --atomic-retarget ${CURRENT_REF}
-ln -sf ${CURRENT_REF} ${CURRENT_REF}.tmplink
-mv -T ${CURRENT_REF}.tmplink current
-
-cd ${WORKDIR}
-umount fs
index 5afea83338c036f177a40be9ea924a53b02fe753..549bbca46efff6ad3720734de82d672b3565328f 100755 (executable)
@@ -50,9 +50,9 @@ class OstbuildDeployQemu(builtins.Builtin):
             target_names.append(target['name'])
 
         helper = privileged_subproc.PrivilegedSubprocess()
-        sys_repo = os.path.join(self.ostree_dir, 'repo')
         shadow_path = os.path.join(self.workdir, 'shadow-repo')
-        helper.spawn_sync(['ostree', '--repo=' + sys_repo,
-                           'pull-local', shadow_path])
+        child_args = ['ostbuild', 'privhelper-deploy-qemu', shadow_path]
+        child_args.extend(target_names)
+        helper.spawn_sync(child_args)
         
-builtins.register(OstbuildDeployRoot)
+builtins.register(OstbuildDeployQemu)
index 16b6bbbb5ee47e0c99076bb48f2e9ddeb1aa7edb..2fc852c96beb083a7651b0e6d95df74f687575b9 100755 (executable)
@@ -24,8 +24,9 @@ from StringIO import StringIO
 
 from . import builtins
 from .ostbuildlog import log, fatal
+from .subprocess_helpers import run_sync
 from . import ostbuildrc
-from . import privileged_subproc
+from . import fileutil
 
 class OstbuildPrivhelperDeployQemu(builtins.Builtin):
     name = "privhelper-deploy-qemu"
@@ -34,23 +35,76 @@ class OstbuildPrivhelperDeployQemu(builtins.Builtin):
     def __init__(self):
         builtins.Builtin.__init__(self)
 
+    def _create_qemu_disk(self):
+        log("%s not found, creating" % (self.qemu_path, ))
+        success = False
+        tmppath = self.qemu_path + '.tmp'
+        if os.path.exists(tmppath):
+            os.unlink(tmppath)
+        subprocess.check_call(['qemu-img', 'create', tmppath, '6G'])
+        subprocess.check_call(['mkfs.ext4', '-q', '-F', tmppath])
+
+        subprocess.call(['umount', self.mountpoint], stderr=open('/dev/null', 'w'))
+        try:
+            subprocess.check_call(['mount', '-o', 'loop', tmppath, self.mountpoint])
+            
+            for topdir in ['mnt', 'sys', 'root', 'home', 'opt', 'tmp', 'run',
+                           'ostree']:
+                path = os.path.join(self.mountpoint, topdir)
+                fileutil.ensure_dir(path)
+            os.chmod(os.path.join(self.mountpoint, 'root'), 0700)
+            os.chmod(os.path.join(self.mountpoint, 'tmp'), 01777)
+
+            varpath = os.path.join(self.mountpoint, 'ostree', 'var')
+            fileutil.ensure_dir(varpath)
+            modulespath = os.path.join(self.mountpoint, 'ostree', 'modules')
+            fileutil.ensure_dir(modulespath)
+            
+            repo_path = os.path.join(self.mountpoint, 'ostree', 'repo')
+            fileutil.ensure_dir(repo_path)
+            subprocess.check_call(['ostree', '--repo=' + repo_path, 'init'])
+            success = True
+        finally:
+            subprocess.call(['umount', self.mountpoint])
+        if success:
+            os.rename(tmppath, self.qemu_path)
+
     def execute(self, argv):
         parser = argparse.ArgumentParser(description=self.short_description)
+        parser.add_argument('srcrepo')
+        parser.add_argument('targets', nargs='+')
 
         args = parser.parse_args(argv)
-        self.args = args
-        
-        self.parse_config()
-        self.parse_bin_snapshot(args.prefix, args.bin_snapshot)
-        
-        target_names = []
-        for target in self.bin_snapshot['targets']:
-            target_names.append(target['name'])
-
-        helper = privileged_subproc.PrivilegedSubprocess()
-        sys_repo = os.path.join(self.ostree_dir, 'repo')
-        shadow_path = os.path.join(self.workdir, 'shadow-repo')
-        helper.spawn_sync(['ostree', '--repo=' + sys_repo,
-                           'pull-local', shadow_path])
+
+        if os.geteuid() != 0:
+            fatal("This helper can only be run as root")
+
+        self.ostree_dir = self.find_ostree_dir()
+        self.qemu_path = os.path.join(self.ostree_dir, "ostree-qemu.img")
+
+        self.mountpoint = os.path.join(self.ostree_dir, 'ostree-qemu-mnt')
+        fileutil.ensure_dir(self.mountpoint)
+
+        if not os.path.exists(self.qemu_path):
+            self._create_qemu_disk()
+
+        subprocess.call(['umount', self.mountpoint], stderr=open('/dev/null', 'w'))
+        repo_path = os.path.join(self.mountpoint, 'ostree', 'repo')
+        try:
+            subprocess.check_call(['mount', '-o', 'loop', self.qemu_path, self.mountpoint])
+            child_args = ['ostree', '--repo=' + repo_path, 'pull-local', args.srcrepo]
+            child_args.extend(args.targets)
+            run_sync(child_args)
+
+            first_target = args.targets[0]
+            for target in args.targets:
+                run_sync(['ostree', '--repo=' + repo_path, 'checkout', '--atomic-retarget', target],
+                         cwd=os.path.join(self.mountpoint, 'ostree'))
+            current_link_path = os.path.join(self.mountpoint, 'ostree', 'current')
+            os.symlink(first_target, current_link_path + '.tmp')
+            os.rename(current_link_path + '.tmp', current_link_path)
+        finally:
+            subprocess.call(['umount', self.mountpoint])
+
         
-builtins.register(OstbuildDeployRoot)
+builtins.register(OstbuildPrivhelperDeployQemu)
index 35d150ad7b2805fde454183157246aed6620b882..7f91e385106ab55ec3bc5223152757735d6e4622 100755 (executable)
@@ -30,8 +30,10 @@ from . import builtin_chroot_compile_one
 from . import builtin_compose
 from . import builtin_compile_one
 from . import builtin_deploy_root
+from . import builtin_deploy_qemu
 from . import builtin_git_mirror
 from . import builtin_pull_components
+from . import builtin_privhelper_deploy_qemu
 from . import builtin_prefix
 from . import builtin_resolve
 from . import builtin_modify_snapshot
@@ -42,6 +44,8 @@ from . import builtin_status
 def usage(ecode):
     print "Builtins:"
     for builtin in builtins.get_all():
+        if builtin.name.startswith('privhelper'):
+            continue
         print "    %s - %s" % (builtin.name, builtin.short_description)
     return ecode
 
index 982984bf9bba434cf6193e9c02db65830c78403f..0116cef2a4c177e776ef92d1cb36797871542fb1 100755 (executable)
@@ -19,6 +19,7 @@ import os,sys,subprocess
 
 from .ostbuildlog import log, fatal
 from . import ostbuildrc
+from .subprocess_helpers import run_sync
 
 class PrivilegedSubprocess(object):
 
@@ -35,5 +36,4 @@ class PrivilegedSubprocess(object):
 
     def _pkexec_spawn_sync(self, argv):
         pkexec_argv = ['pkexec'] + argv
-        log("Running: %s" % (subprocess.list2cmdline(pkexec_argv), ))
-        subprocess.check_call(pkexec_argv)
+        run_sync(pkexec_argv)